package com.yxt.service.impl;

import com.yxt.mapper.TbCentreMapper;
import com.yxt.mapper.TbUserMapper;
import com.yxt.pojo.ResponseEntity;
import com.yxt.pojo.TbCentre;
import com.yxt.pojo.TbUser;
import com.yxt.service.UserService;
import com.yxt.utils.IdWorker;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    TbUserMapper userMapper;
    @Resource
    TbCentreMapper tbCentreMapper;
    @Resource
    StringRedisTemplate stringRedisTemplate;
    @Resource
    IdWorker idWorker;

    @Override
    public ResponseEntity login(TbUser tbUser,HttpServletRequest request) {
        String token1 = request.getHeader("token");

        TbUser user=userMapper.select(tbUser.getUname());
        if(user==null){
            return new ResponseEntity(false,300,"用户名或密码错误",null);
        }
        if(!user.getUpwd().equals(tbUser.getUpwd())){
            return new ResponseEntity(false,300,"用户名或密码错误",null);
        }
        if(user.getState()==2){
            return new ResponseEntity(false,300,"当前用户已注销，无法登录",null);
        }
        String string = user.getUname();
        String token = Jwts.builder().setId(user.getId() + "").setSubject(user.getUname())
                .signWith(SignatureAlgorithm.HS256,string).compact();

//        String tokens = stringRedisTemplate.opsForValue().get(token);

//        if(tokens.equals(token1)){
//            return new ResponseEntity(false,300,"当前用户以登录，请勿重复登录",null);
//        }else {
            stringRedisTemplate.opsForValue().set("token",token,2, TimeUnit.MINUTES);
//        }

        return new ResponseEntity(token);
    }

    @Override
    public ResponseEntity register(TbUser tbUser) {

        tbUser.setState(1);
        tbUser.setCreatTime(new Date());
        userMapper.insertSelective(tbUser);
        TbUser user = userMapper.select(tbUser.getUname());

        TbCentre tbCentre=new TbCentre();
        tbCentre.setUid(user.getId());
        tbCentre.setRoleid(user.getRoleId());
        tbCentre.setPowerid(3);
        tbCentre.setDepid(6);
        tbCentreMapper.insertSelective(tbCentre);
        return new ResponseEntity();
    }

    @Override
    public ResponseEntity del(Integer uid) {

            TbUser tbUser1=new TbUser();
            tbUser1.setId(uid);
            tbUser1.setState(2);
            userMapper.updateByPrimaryKeySelective(tbUser1);

        return new ResponseEntity();
    }

    @Override
    public ResponseEntity gdel(Integer id, Integer uid) {
        TbUser selects = userMapper.selects(id);
        if(selects.getRoleId()==3){
            return new ResponseEntity(false,3000,"用户权限不足",null);
        }else {
            TbUser tbUser=new TbUser();
            tbUser.setId(uid);
            tbUser.setState(2);
            userMapper.updateByPrimaryKeySelective(tbUser);
        }
        return new ResponseEntity();
    }

    @Override
    public ResponseEntity list(TbUser tbUser) {
        List<TbUser> tbUsers = userMapper.selectByExample(null);
        return new ResponseEntity(tbUsers);
    }

    @Override
    public ResponseEntity update(Integer pid, Integer id,Integer uid) {
        TbUser selects = userMapper.selects(id);

        if(selects.getRoleId()==1 || selects.getRoleId()==2 ){
            return new ResponseEntity(false,3000,"用户权限不足",null);
        }else {
            TbCentre tbCentre1 = new TbCentre();
            tbCentre1.setId(uid);
            tbCentre1.setPowerid(pid);
            tbCentreMapper.updateByPrimaryKeySelective(tbCentre1);
        }
        return new ResponseEntity();
    }
}
